package com.xtx.service.impl;

import com.xtx.entity.CategoryChildren;
import com.xtx.entity.CategorySubParent;
import com.xtx.mapper.CategoryMapper;
import com.xtx.service.CategoryService;
import com.xtx.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /*
    * @Description: 通过id获取分类信息
     */
    @Override
    public List<CategoryVo> getCategoryById(Integer id) {
        //获取一级分类信息
        List<CategoryVo> list = categoryMapper.getCategoryById(id);
        //根据一级分类信息id获取二级分类信息
        List<CategoryChildren> children = categoryMapper.getCategoryChildrenId(id);
        //循环遍历一级分类信息，将二级分类信息插入到一级分类信息中
        list.forEach(category -> category.setChildren(children));
        return list;
    }

    /*
    * @Description: 通过id获取分类下的商品信息
    * 获取分类下的商品信息
     */
    @Override
    public List<CategoryGoodsVo> getCategoryGoodsById(Integer id) {
        List<CategoryGoodsVo> list = categoryMapper.CategoryChildrenById(id);
        list.forEach(catgory->{
            catgory.setGoods(categoryMapper.getCategoryGoods(catgory.getId()));
        });
        return list;
    }
    /*
    * @Description: 通过id获取分类下的商品信息
    * 获取二级分类下的商品信息
     */
    @Override
    public List<CategoryGoodsSubVO> getCategorySubById(Integer id) {
        CategorySubParent parent = categoryMapper.getCategorySubParentById(id);
        List<CategoryGoodsSubVO> list = categoryMapper.CategoryChildrenSubById(id);
        list.forEach(catgory->{
            catgory.setGoods(categoryMapper.getCategoryGoodsSubById(catgory.getId()));

        });
        BeanUtils.copyProperties(parent,list.get(0));
        return list;
    }
}
